<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">

    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">

        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->

    <el-row>

        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>

    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible"
            width="30%"
            >
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--修改数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="EditdialogVisible"
            width="30%"
    >
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="updateEdit">提交</el-button>
                <el-button @click="EditEditdialogVisibleFalse">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    label="序号"
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="primary"
                            plain @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                             <!--plain @click="dialogVisible = true">修改</el-button>-->
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
               <!-- <el-row>
                    <el-button type="primary"  size="mini" >修改</el-button>
                    <el-button type="danger"   size="mini" plain @click="deleteById">删除</el-button>
                </el-row>-->

            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        mounted(){
            //当页面加载完成后，发送异步请求 获取数据
            this.selectAll();
    /*        var _this = this;
            axios({
                method:"get",
                url:"http://localhost:8080/brand-case/selectAllServlet"
            }).then(function (resp){
                console.log("测试完成进入数据赋值")
                _this.tableData= resp.data;
                console.log("测试完成")
                console.log(resp.data)
            })*/
        },
        methods: {
            selectAll(){

            /*    var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/brand-case/brand/selectAll"
                }).then(function (resp){
                    _this.tableData= resp.data;
                })*/

               /* var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/brand-case/brand/selectByPage?currentPage="+ _this.currentPage+"&pageSize=" + _this.pageSize
                }).then(function (resp){
                    //设置表格数据
                    _this.tableData= resp.data.rows;
                    //获取总数数据
                    _this.totalCount= resp.data.totalCount;
                })*/

           /*     var _this = this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage="+ this.currentPage+"&pageSize=" + this.pageSize,
                    data:this.brand
                }).then(function (resp){
                    //设置表格数据
                    _this.tableData= resp.data.rows;
                    //获取总数数据
                    _this.totalCount= resp.data.totalCount;
                })*/

                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage="+ this.currentPage+"&pageSize=" + this.pageSize,
                    data:this.brand
                }).then(resp =>{
                    //设置表格数据
                    this.tableData= resp.data.rows;
                    //获取总数数据
                    this.totalCount= resp.data.totalCount;
                })
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;

                console.log(this.multipleSelection)
            },
            // 查询方法
            onSubmit() {
                console.log(this.brand);
                this.selectAll();
            },
            // 添加数据
            addBrand(){
               /* var _this = this;*/
                //发送ajax请求
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand/add",
                    data:this.brand
                   }).then(resp =>{
                    if(resp.data == "success"){
                        //关闭窗口
                        this.dialogVisible = false;
                        //重新查询数据
                        this.brand.brandName = "";
                        this.brand.companyName = "";
                        this.brand.status = "";
                        this.brand.ordered = "";
                        this.brand.description = "";
                        this.currentPage = 1;
                        this.size      = 5;
                        this.selectAll();
                        //弹出消息提示
                        this.$message({
                            message: '恭喜你提交数据成功',
                            type: 'success'
                        });
                    } else {
                        //关闭窗口
                        this.dialogVisible = false;
                        console.log("添加数据测试失败")
                        //弹出消息提示
                        this.$message({
                            message: '恭喜你提交数据失败' + resp.data,
                            type: 'warning'
                        });
                    }
                })
            },
            //分页
            handleSizeChange(val) {
                //console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.selectAll();
            },
            handleCurrentChange(val) {
                //console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.selectAll();
            },
            //批量删除数据
            deleteByIds(){
                //弹出确认弹窗
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                 //用户点击确定按钮
                    //创建ID数据
                    for (let i = 0;i<this.multipleSelection.length;i++){
                        let selectionElement = this.multipleSelection[i];
                        this.selectedIds[i] = selectionElement.id;
                    }
                    //发送 ajax请求servlet
                    var _this = this;
                    //发送ajax请求
                    axios({
                        method:"post",
                        url:"http://localhost:8080/brand-case/brand/deleteByIds",
                        data:_this.selectedIds
                    }).then(function (resp){
                        if(resp.data == "success"){
                            //关闭窗口
                            _this.dialogVisible = false;
                            //重新查询数据
                            _this.selectAll();
                            //弹出消息提示
                            _this.$message({
                                message: '恭喜你删除数据成功',
                                type: 'success'
                            });
                        } else {
                            //关闭窗口
                            _this.dialogVisible = false;
                            //弹出消息提示
                            _this.$message({
                                message: '删除数据失败' + resp.data,
                                type: 'warning'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            handleEdit(index, row) {
                console.log("测试")
                console.log("测试",row.id)
                console.log(index, row);
                var _this = this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/brand-case/brand/selectById?id="+ row.id
                }).then(function (resp){
                    //设置表格数据
                    //_this.tableData= resp.data;
                    console.log(resp.data)
                    _this.brand = row;
                    _this.EditdialogVisible = true;


                })
            },

            updateEdit() {
                /* var _this = this;*/
                //发送ajax请求
                axios({
                    method:"post",
                    url:"http://localhost:8080/brand-case/brand/update",
                    data:this.brand
                }).then(resp =>{
                    if(resp.data == "success"){
                        //关闭窗口
                        this.EditdialogVisible = false;
                        //重新查询数据
                        this.brand.brandName = "";
                        this.brand.companyName = "";
                        this.brand.status = "";
                        this.brand.ordered = "";
                        this.brand.description = "";
                        //this.currentPage = 1;
                        //this.size      = 5;

                        this.selectAll();
                        //弹出消息提示
                        this.$message({
                            message: '恭喜你提交数据成功',
                            type: 'success'
                        });
                    } else {
                        //关闭窗口
                        this.EditdialogVisible = false;
                        console.log("修改数据测试失败")
                        //弹出消息提示
                        this.$message({
                            message: '修改数据失败' + resp.data,
                            type: 'warning'
                        });
                    }
                })
            },
            handleDelete(index, row) {
                console.log("测试",row.id)
                console.log(index, row);
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //用户点击确定按钮
                    //创建ID数据
                    console.log("进入删除数据：test");
                    //发送ajax请求
                    axios({
                        method:"post",
                        url:"http://localhost:8080/brand-case/brand/deleteById?id=" + row.id,
                        //data:this.row.id
                    }).then(resp =>{
                        console.log("删除数据测试完成进入数据赋值：" + resp.data)
                        if(resp.data == "success"){
                            //关闭窗口
                            console.log("删除数据测试完成进入数据赋值 关闭窗口")
                            this.dialogVisible = false;
                            //重新查询数据
                            this.selectAll();
                            //弹出消息提示
                            this.$message({
                                message: '恭喜你删除数据成功',
                                type: 'success'
                            });
                        } else {
                            //关闭窗口
                            this.dialogVisible = false;
                            //弹出消息提示
                            this.$message({
                                message: '删除数据失败' + resp.data,
                                type: 'warning'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            EditEditdialogVisibleFalse(){
                //关闭窗口
                this.EditdialogVisible = false;
                //重新查询数据
                this.brand.brandName = "";
                this.brand.companyName = "";
                this.brand.status = "";
                this.brand.ordered = "";
                this.brand.description = "";
                //this.currentPage = 1;
                //this.size      = 5;

                this.selectAll();
            }


        },
        data() {
            return {
                //每页展示数据
                pageSize:5,
                //总记录数
                totalCount: 100,
                // 当前页码
                currentPage: 1,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                EditdialogVisible:false,

                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                },
                //被选中ID数组
                selectedIds:[],
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }]
            }
        }
    })

</script>

</body>
</html>